from django.shortcuts import redirect, reverse
from django.urls import resolve
from django.utils.deprecation import MiddlewareMixin

from Jv.settings import REQUIRE_LOGIN_URL


class LoginMiddlewareMixin(MiddlewareMixin):
    """登录判定中间件"""

    def process_request(self, request):
        if resolve(request.path_info).url_name in REQUIRE_LOGIN_URL:
            if not request.user.is_authenticated:
                return redirect(reverse('home'))

    def process_response(self, request, response):
        return response
